package com.neuedu;

import java.sql.*;

public class HiveToMySQLA {
    public static void main(String[] args) {
        String hiveJdbcUrl = "jdbc:hive2://192.168.47.101:10000/vehicledb"; // Hive JDBC URL
        String mysqlJdbcUrl = "jdbc:mysql://192.168.47.101:3306/vehicledb"; // MySQL JDBC URL
        String mysqlUser = "root";
        String mysqlPassword = "123456";

        Connection hiveConn = null;
        Connection mysqlConn = null;
        Statement hiveStmt = null;
        Statement mysqlStmt = null;
        ResultSet rs = null;

        try {
            // 1. 连接到 Hive
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            hiveConn = DriverManager.getConnection(hiveJdbcUrl, "", "");
            hiveStmt = hiveConn.createStatement();

            // 2. 从 Hive 查询数据
            String hiveQuery = "SELECT model, units_sold, make, low_price, high_price, times, is_ev, body_type, brand, brand_country FROM a_china_automobile_sales";
            rs = hiveStmt.executeQuery(hiveQuery);

            // 3. 连接到 MySQL
            Class.forName("com.mysql.cj.jdbc.Driver");
            mysqlConn = DriverManager.getConnection(mysqlJdbcUrl, mysqlUser, mysqlPassword);
            mysqlStmt = mysqlConn.createStatement();

            // 4. 创建 MySQL 插入语句
            String insertQuery = "INSERT INTO a_china_automobile_sales (model, units_sold, make, low_price, high_price, times, is_ev, body_type, brand, brand_country) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            // 使用 PreparedStatement 以防止 SQL 注入
            PreparedStatement mysqlStmtPrepared = mysqlConn.prepareStatement(insertQuery);

            // 5. 将数据从 Hive 导入 MySQL
            while (rs.next()) {
                String model = rs.getString("model");
                int unitsSold = rs.getInt("units_sold");
                String make = rs.getString("make");
                double lowPrice = rs.getDouble("low_price");
                double highPrice = rs.getDouble("high_price");
                Date times = rs.getDate("times");
                String isEv = rs.getString("is_ev");
                String bodyType = rs.getString("body_type");
                String brand = rs.getString("brand");
                String brandCountry = rs.getString("brand_country");

                // 设置参数
                mysqlStmtPrepared.setString(1, model);
                mysqlStmtPrepared.setInt(2, unitsSold);
                mysqlStmtPrepared.setString(3, make);
                mysqlStmtPrepared.setDouble(4, lowPrice);
                mysqlStmtPrepared.setDouble(5, highPrice);
                mysqlStmtPrepared.setDate(6, times);
                mysqlStmtPrepared.setString(7, isEv);
                mysqlStmtPrepared.setString(8, bodyType);
                mysqlStmtPrepared.setString(9, brand);
                mysqlStmtPrepared.setString(10, brandCountry);

                // 执行插入
                mysqlStmtPrepared.executeUpdate();
            }

            System.out.println("Data successfully transferred from Hive to MySQL");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) rs.close();
                if (hiveStmt != null) hiveStmt.close();
                if (hiveConn != null) hiveConn.close();
                if (mysqlStmt != null) mysqlStmt.close();
                if (mysqlConn != null) mysqlConn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

